﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Windows.Forms
{
    /// <summary>
    /// 表示窗体基类
    /// </summary>
    /// <creator>marc</creator>
    public class FormBase : Form
    {
        /// <summary>
        /// 按下<see cref="Keys.Escape"/>键是否关闭窗体，默认：<see langword="true"/>
        /// </summary>
        protected bool EscapeCanQuit { get; set; } = true;

        /// <inheritdoc cref="FormBase"/>
        protected FormBase()
        {
            Initialize();

            DoubleBuffered = true;
        }

        /// <summary>
        /// 在构造函数中初始化
        /// </summary>
        protected virtual void Initialize()
        {

        }

        /// <inheritdoc/>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (EscapeCanQuit)
            {
                if (keyData == Keys.Escape)
                {
                    this.Close(); // 关闭窗体
                    //this.Dispose();//释放窗体不合适，因为窗体的右上角的X关闭按钮，并不会调用Dispose()方法，我们这里也不宜调用Dispose()
                    return true; // 表示已处理该按键
                }
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }
    }

}
